﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Website.Kunde
{
    public partial class Anmeld : System.Web.UI.Page
    {

        #region Public Properties

        public int ServiceCarScheduleDetailId
        {
            get
            {
                if (ViewState["_servicedetail"] != null)
                    return (int)ViewState["_servicedetail"];
                else
                    return 0;
            }
            set
            {
                ViewState["_servicedetail"] = value;
            }
        }

        public string CustomerName
        {
            get
            {
                if (ViewState["_customername"] != null)
                    return (string)ViewState["_customername"];
                else
                    return "";
            }
            set
            {
                ViewState["_customername"] = value;
            }
        }

        public string CustomerCity
        {
            get
            {
                if (ViewState["_customercity"] != null)
                    return (string)ViewState["_customercity"];
                else
                    return "";
            }
            set
            {
                ViewState["_customercity"] = value;
            }
        }

        public int CustomerZipCode
        {
            get
            {
                if (ViewState["_zipcode"] != null)
                    return (int)ViewState["_zipcode"];
                else
                    return 0;
            }
            set
            {
                ViewState["_zipcode"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                if (Request.QueryString["Id"] != null)
                {
                    TireServiceDB db = new TireServiceDB();
                    try 
	                {
                        ServiceCarScheduleDetailView detail = db.ServiceCarScheduleDetailViews.SingleOrDefault(p => p.ServiceCarScheduleDetailId == Convert.ToInt32(Request.QueryString["Id"].ToString()));

                        if (detail != null) 
                        {
                            if (detail.CustomerId == db.GetCustomerIdByUserId(new Guid(Membership.GetUser().ProviderUserKey.ToString())))
                            {
                                if (detail.CustomerRated)
                                {
                                    // Already rated
                                }
                                else
                                {
                                    // Not rated yet


                                    // Set page properties for editing
                                    this.ServiceCarScheduleDetailId = detail.ServiceCarScheduleDetailId;
                                    this.CustomerName = detail.Name;
                                    this.CustomerZipCode = Convert.ToInt32(detail.CustomerZipCode);
                                    this.CustomerCity = detail.CustomerCity;
                                }
                            }
                            else
                            {
                                // Service detail do not belong to current customer - redirect customer to startpage
                                Response.Redirect("~/Kunde/Index.aspx");
                            }
                        }
                        else
                        {
                            // Service detail not found - redirect customer to startpage
                            Response.Redirect("~/Kunde/Index.aspx");
                        }
	                }
	                catch (Exception ex)
	                {
		                // Exception occured - redirect customer to startpage
                        Response.Redirect("~/Kunde/Index.aspx");
	                }
                    
                }
                else
                {
                    // Redirect to customer startpage
                    Response.Redirect("~/Kunde/Index.aspx");
                }    
            }
        }

        /// <summary>
        /// Button function that sends email to us with request from user to be contacted
        /// </summary>
        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ServiceCarScheduleDetailId != 0)
                {
                    TireServiceDB db = new TireServiceDB();

                    // Update detail
                    ServiceCarScheduleDetail detail = db.GetServiceCarScheduleDetail(this.ServiceCarScheduleDetailId);
                    detail.CustomerRated = true;

                    // Create new rating
                    ServiceCarScheduleDetailRating rating = new ServiceCarScheduleDetailRating();
                    detail.ServiceCarScheduleDetailRatings.Add(rating);
                    rating.Rating = Convert.ToInt32(rcServiceRate.Value);
                    if (txtComment.Text != "") rating.Comment = txtComment.Text;
                    rating.Name = this.CustomerName;
                    rating.ZipCode = this.CustomerZipCode;
                    rating.City = this.CustomerCity;

                    db.AddServiceCarScheduleDetailRating(rating);

                    // Commit changes to database
                    db.SubmitChanges();

                    // Dispose connection to database
                    db.Dispose();

                    // Redirect customer to service overview with rating
                    Response.Redirect("~/Kunde/Hjulskift.aspx");
                }
            }
            catch (Exception ex)
            {
                //if ((ex) is ArgumentException)
                //{
                //    // User error - arguments not valid - show error message
                //    lMessage.Text = ((ArgumentException)ex).Message;
                //}
                //else
                //{
                //    // System error - show general error message
                //    lMessage.Text = "Fejl opstod - venligst prøv igen";
                //}
            }
        }
    }
}